<?php
class Common_model extends CI_Model{
    function  __construct(){
        parent::__construct();
    }
    function getdata($table,$where='',$select='',$order='',$limit=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function curPageURL() {
        $pageURL='';
         //$pageURL = 'http';
        // $pageURL .= "://m.";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }
    function getlist($table,$where_in = array(),$where='',$select='',$order='',$limit=''){
        if(!empty($where_in)){
            $this->db->where_in($where_in[0],$where_in[1]);
        }
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function get_data($table,$where='',$order='',$limit='',$select='',$srart_limit=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order['truong'],$order['sapxep']);
        }
        if(!empty($limit)){
            if(empty($srart_limit)){
                $this->db->limit($limit);
            }else $this->db->limit($limit,$srart_limit);
            
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data;
        }else return false;
    } 
    function get_data_news($table,$where='',$order='',$limit='',$select='',$srart_limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);   
        }
        if(!empty($order)){
            $this->db->order_by($order['truong'],$order['sapxep']);
        }
        if(!empty($limit)){
            if(empty($srart_limit)){
                $this->db->limit($limit);
            }else $this->db->limit($limit,$srart_limit);    
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data;
        }else return false;
    }
    function getdatain($table,$where=array(),$order='',$limit=''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        if(!empty($order)){
            $this->db->order_by($order[0],$order[1]);
        }
        if(!empty($limit)){
            if(is_array($limit)){
                $this->db->limit($limit[0],$limit[1]);
            }else $this->db->limit($limit);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->result();
            $query->free_result();
            return $data; 
        }else return false;
    }
    function getone($table,$where='',$select=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($select)){
            $this->db->select($select);
        }
        $query = $this->db->get($table);
        if($query->num_rows() > 0){
            $data = $query->row();
            $query->free_result();
            return $data;
        }else return false;
    }
    function insertdata($table,$data=array()){
        $this->db->insert($table, $data); 
    }
    function updatedata($table,$where ='',$data=array()){
        $this->db->where($where);
        $this->db->update($table, $data);
    }
    function deletedata($table,$where){
        $this->db->where($where);
        $this->db->delete($table);
    }
    function getnumber($table,$where = ''){
        if(!empty($where)){
            $this->db->where_in($where[0],$where[1]);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
     function getnumrow($table,$where_in=array(),$where = ''){
        if(!empty($where_in)){
            $this->db->where_in($where_in[0],$where_in[1]);
        }
        if(!empty($where)){
            $this->db->where($where);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
    function getnumber2($table,$where=''){
        if(!empty($where)){
            $this->db->where($where);
        }
        $query = $this->db->get($table);
        return $query->num_rows();
    }
    function getsum($fiel,$where,$table){
        $this->db->select_sum($fiel);
        $this->db->where($where);
        $query = $this->db->get($table);
        return $query->row()->$fiel;
    }
    function check_trung($nd,$table){
        $this->db->where($nd);
        $query = $this->db->get($table);
        if($query->num_rows()>0){
            $query->free_result();
            return true;
        }return false;
    }
     function category_tt($id,$mangcat,$dt,$i=0){   
        if($i==0){$this->dt = array();}
        $i++;        
        $this->dt[]=$mangcat[$id]->id;    
        if($mangcat[$id]->parentid!=0){    
            $this->category_tt($mangcat[$id]->parentid,$mangcat,$this->dt,$i);           
        }
        $i--;
        if($i==0){
            return array_reverse($this->dt);
        }
    }
    function category_tt1($id,$mangcat,$dt1,$i=0){   
        if($i==0){$this->dt1 = array();}
        $i++;        
        $this->dt1[]=$mangcat[$id];    
        if($mangcat[$id]->parentid!=0){    
            $this->category_tt1($mangcat[$id]->parentid,$mangcat,$this->dt1,$i);           
        }
        $i--;
        if($i==0){
            return array_reverse($this->dt1);
        }
    }
    function  getcat($mangcat,$id,$dtt,$i=0){  
        
        if($i==0){$this->dtt = array();}
        $i++;
        $this->dtt[] = $id;
        if(!empty($mangcat[$id])){
            foreach ($mangcat[$id] as $m){
                $this->getcat($mangcat,$m->id,$this->dtt,$i);
            }
        }
        $i--;
        if($i==0){ 
          return $this->dtt;  
        } 
    }
}